import { OnInit, Component, Input, ViewChild, ElementRef, EventEmitter, Output, OnChanges, SimpleChanges } from '@angular/core';
import { NzMessageService, NzModalService, UploadXHRArgs, UploadFile, InputNumber, UploadChangeParam, InputBoolean } from 'ng-zorro-antd';
import { HttpService } from 'lbf';
import { Observable, Observer, Subscription } from 'rxjs';
import Viewer from 'viewerjs';
import { VideoPlatService } from '@core/lb/VideoPlatService';

import { LbBzbqComponent } from '@shared/components/lb-bzbq/lb-bzbq.component';
import { ClickOutsideDirective } from 'ngx-image-gallery';
import { fstat } from 'fs';

@Component({
  selector: 'lb-wjfw',
  templateUrl: './lbwjfw.component.html',
  styleUrls: ['./lbwjfw.component.less']
})
export class LbWjfwComponent implements OnInit, OnChanges {

  // @Input() @InputNumber() DAD069: number;
  // @Input() @InputNumber() DAD028: number;
  // @Input() @InputNumber() DAD017: number;

  @Input() snames: FileSname;
  @Input() param: any;
  // LL 最大文件数
  @Input() maxFileCount: any = 999;

  @ViewChild("imglist", { static: false }) imgList: ElementRef;
  /**
   * 图表列表
   */
  fileList = [];

  /**
   * 上传按钮是否显示
   */
  @Input() @InputBoolean() showButton = true;

  /**
   * 文件上传成功后回调
   */
  @Output() addDone = new EventEmitter();
  /**
   * 文件删除后回调
   */
  @Output() delDone = new EventEmitter();

  isVisible = false
  viewer;

  constructor(private msgsvr: NzMessageService,
    private vp: VideoPlatService,
    private lbservice: HttpService,
    private modalService: NzModalService, ) {

  }
  ngOnChanges(changes: SimpleChanges): void {



    this.getFiles();


  }

  getFiles(): void {

    this.lbservice.lbservice('YBCL_QUERY', { para: { PROJID: this.param } }).then(resdata => {

      if (resdata.code < 1) {
        this.msgsvr.error(resdata.errmsg);
        return;
      }

      const flist: any[] = resdata.message.list;
      const list = [];

      flist.forEach(f => {
        list.push({ name: f.STUFFNAME, url: f.ATTACHPATH, })
      })

      this.fileList = list;
    })
  }

  ngOnInit(): void {
    this.getFiles();
  }

  imgClick(img) {

    if (this.viewer) {
      this.viewer.destroy()
    }
    this.viewer = new Viewer(this.imgList.nativeElement);
    this.viewer.show();
  }

  uploadChanged(event) {


    if (event.type === "success") {
      const resdata = event.file.response;
      if (resdata.code < 1) {
        this.msgsvr.error(resdata.errmsg);
      } else {

        const filelist = resdata.message.list;
        const list = [];

        filelist.forEach(f => {
          list.push({ name: f.STUFFNAME, url: f.ATTACHPATH, })
          //this.addDone.emit({ ...this.param, CZE001: f.CZE001 })
        })


        this.fileList = [...this.fileList, ...list];
        this.isVisible = false;
        this.msgsvr.success('上传成功！');

      }
    }

  }

  blbz() {
    const modal = this.modalService.create({
      nzTitle: '补正补齐',
      nzContent: LbBzbqComponent,
      nzComponentParams: {
        param: this.param,
      },
      nzWidth: 880,
      nzOnOk: () => {
        this.fs();
      }
    });
  }

  fs() {
    this.lbservice.lbservice('P_8004', { para: this.param }).then(resdata => {
      if (resdata.code < 1) {
        this.msgsvr.error(resdata.errmsg);
      }
      else {
        this.msgsvr.success("成功");
      }
    })

  }
}



interface FileSname {
  /**
   * 文件上传
   */
  save?: string;
  /**
   * 文件预览
   */
  query: string;
  /**
   * 文件删除
   */
  del?: string;
}